<template>
  <div id="app">
    <form action="#" v-show="isshow">
      <h1>新增商品</h1>
      <div>
        <label>品牌名称:</label>
        <input type="text" v-model="name" />
      </div>
      <div>
        <label>创立时间:</label>
        <input type="text" v-model="time" />
      </div>
      <div>
        <button @click="fn">确定提交</button>
      </div>
    </form>
    <button class="btn" @click="isshow = true">新增</button>
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <!-- 循环渲染的元素tr -->
      <tr v-for="item in list" :key="item.id">
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.time }}</td>
        <td>
          <button @click="del(item.id)">删除</button>
        </td>
      </tr>
      <tr v-if="list.length===0">
        <td colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
</template>

<script>
export default {
  name: "APP",
  data() {
    return {
      list: [{ id: 1, name: "奔驰", time: "2020-08-01" }],
      isshow: false,
      name: "",
      time: "",
    };
  },
  methods: {
    // 将输入框的内容提交渲染到表单
    fn() {
      this.list.push({
        id: this.list.length + 1,
        name: this.name,
        time: this.time,
      });
      // 关闭输入框
      this.isshow=false;
      this.name = "", 
      this.time = "";
      
    },
    // 删除部分
    del(id) {
      // 将没有被点击的数据传到新的数组里面
      // this.list = this.list.filter((item) => item.id !== id);
     let newArr= this.list.splice(id,1)
     this.list=newArr
    },
  },
};
</script>

<style>
#app {
  width: 600px;
  margin: 10px auto;
}
.btn {
  border: 1px solid #ccc;
  margin-bottom: 10px;
  margin-top: 40px;
}

.tb {
  border-collapse: collapse;
  width: 100%;
}

.tb th {
  background-color: #0094ff;
  color: white;
}

.tb td,
.tb th {
  padding: 5px;
  border: 1px solid black;
  text-align: center;
}

.add {
  padding: 5px;
  border: 1px solid black;
  margin-bottom: 10px;
}
</style>
